﻿using Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools;
using 纺织贸易管理系统;

namespace 后整理管理系统.其他窗体
{
    public partial class 工艺二次登记 : Sunny.UI.UIForm
    {
        private string Liuzhuanka { get; set; }
        public List <liuzhuanmingxi > Liuzhuanmingxis { get; set; }
        private FormUseful useful;
        public 工艺二次登记(string liuzhuanka,FormUseful formUseful)
        {
            InitializeComponent();
          this. Liuzhuanka = liuzhuanka;
            txtliuzhuankahao.Text = this.Liuzhuanka;
           var liuzhuangkamingxi = liuzhuanmingxiService.Getliuzhuanmingxilst(x => x.CardNum == liuzhuanka);
            gridControl1.DataSource = liuzhuangkamingxi;
            gridControl1.RefreshDataSource();
            this.useful = formUseful;
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridView1.CloseEditor();
            if (useful == FormUseful.修改)
            {
                var liuzhuankamingxi = gridControl1.DataSource as List<liuzhuanmingxi>;
                try
                {
                    Connect.dbcontext.BeginTran();
                    Connect.dbcontext.Updateable(liuzhuankamingxi).ExecuteCommand();
                    Connect.dbcontext.Insertable<DeleteLog>(new DeleteLog() { date = DateTime.Now, Log = "二次填写流转卡工艺：" + Liuzhuanka, useID = Tools.CurrentUser.user.YHBH, Reason = "工艺二次登记" });
                    Connect.dbcontext.CommitTran();
                    txtliuzhuankahao.Text = string.Empty;
                    UINotifierHelper.ShowNotifier("保存成功", UINotifierType.OK, "工艺二次登记保存成功");
                    liuzhuankamingxi.Clear();
                    gridControl1.DataSource = liuzhuankamingxi;
                    gridControl1.RefreshDataSource();
                }
                catch (Exception ex)
                {
                    Connect.dbcontext.RollbackTran();
                    ShowMessageBox.ShowErrorBox("保存的时候发生了错误！" + ex.Message);
                }
            }
            else
            {
                this.Liuzhuanmingxis = gridControl1.DataSource as List<liuzhuanmingxi>;
                this.Close();
            }
        }

        private void txtliuzhuankahao_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter)
            {
                var liuzhuangkamingxi = liuzhuanmingxiService.Getliuzhuanmingxilst(x => x.CardNum == txtliuzhuankahao.Text );
                gridControl1.DataSource = liuzhuangkamingxi;
                gridControl1.RefreshDataSource();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var liuzhuangkamingxi = liuzhuanmingxiService.Getliuzhuanmingxilst(x => x.CardNum == txtliuzhuankahao.Text);
            gridControl1.DataSource = liuzhuangkamingxi;
            gridControl1.RefreshDataSource();
        }
    }
}
